/*
 * Java CMPP API
 * Copyright (C) 2006 by Youming.Xu
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 526 Room, Hong Wa Building,22#,South Section1,
 * YiHuan Road,Sichuan,Chengdu,P.R.China
 * 
 * A copy of the LGPL can be viewed at http://www.gnu.org/copyleft/lesser.html
 * Java CMPP API author: xym008@gmail.net
 * Java CMPP API Homepage: http://cmppapi.sourceforge.net/
 * BindResp.java,v 1.10 2006-7-416:34:37 xuym Exp
 */

package com.bci.cmpp.message;

import com.bci.cmpp.util.CMPPIO;

/**
 * BindResp.java
 * 
 * @author xuym
 * @version 1.0
 * 
 */
public class CMPPConnectResp30 extends CMPPConnectResp {

	/**
	 * 
	 */
	public CMPPConnectResp30() {
		super();
		this.totalLength = 33;
		this.buffer = new byte[this.totalLength];
	}

	/***/
	public CMPPConnectResp30(byte[] data) throws InvalidParameterValueException {
		this();
		// this.validate();
		if (data == null || data.length < this.totalLength)
			throw new InvalidParameterValueException("input data is invalid.",
					"");
		this.readFrom(data, 0);
	}

	public byte[] getBytes() {
		// if (this.buffer == null || this.buffer.length != this.totalLength) {
		CMPPIO.intToBytes(this.totalLength, 4, this.buffer, 0);
		CMPPIO.intToBytes(this.commandId, 4, this.buffer, 4);
		CMPPIO.intToBytes(this.sequenceNum, 4, this.buffer, 8);
		CMPPIO.intToBytes(this.commandStatus, 4, this.buffer, 12);
		/** AuthenticatorISMG */
		validate();
		System.arraycopy(authenticatorISMG, 0, this.buffer, 16, authenticatorISMG.length);
		/** end of AuthenticatorISMG */
		CMPPIO.intToBytes(this.version, 1, this.buffer, 32);
		// buffer[29] = CMPPVersion.V30.getVersionID();
		// }
		return this.buffer;
	}

	public void readFrom(byte[] data, int offset)
			throws InvalidParameterValueException {
		if (data == null || data.length < this.totalLength + offset)
			throw new InvalidParameterValueException(
					"input data is not validate.", "");
		this.sequenceNum = CMPPIO.bytesToInt(data, 8 + offset, 4);
		this.commandStatus = CMPPIO.bytesToInt(data, 12 + offset, 4);
		/** get the authenticatorISMG */
		authenticatorISMG = new byte[16];
		System.arraycopy(data, 16 + offset, authenticatorISMG, 0, authenticatorISMG.length);
		this.version = CMPPIO.bytesToInt(data, 32 + offset, 1);
		System.arraycopy(data, offset, this.buffer, 0, this.totalLength);

	}

}
